#include<bits/stdc++.h>
using namespace std;
const int N=8005,M=30005;
bool f[M*2]={false};
vector<int>good_num;
bool isGood[M*2]={false};
void init(){
	memset(f,true,sizeof f);
	f[0]=f[1]=false;
	for(int i=2;i<=60000;i++)
		for(int j=i+i;j<=60000;j+=i)
			f[j]=false;
	for(int i=1;i<=60000;i++)
		if(f[i]&&(i%20==3||i<=10))
			good_num.push_back(i),isGood[i]=true;
    return;
}
int n,m;
struct Pnt {
	int x,y;
	Pnt (int _x=0,int _y=0):
		x(_x),y(_y){}
} p[N];
bool operator<(Pnt a,Pnt b){
	return a.x==b.x?a.y<b.y:a.x<b.x;
}
int val[N]={0};
int dis1(Pnt a,Pnt b){
	return abs(a.x-b.x)+abs(a.y-b.y);
}
int dis2(Pnt a,Pnt b){
	return max(abs(a.x-b.x),abs(a.y-b.y));
}
Pnt dec(Pnt a){
	return Pnt(a.x+a.y+m*3,a.x-a.y+m*3);
}
struct Q{
	int lx,rx;
	int ly,ry;
	Q(int _lx=0,int _rx=0,int _ly=0,int _ry=0):
		lx(_lx),rx(_rx),ly(_ly),ry(_ry){}
};
vector<Q>sq;
Q build_up(Pnt a,int r){
	return Q(a.x-r,a.x+r,a.y-r,a.y+r);
}
Q intersect(Q a,Q b){
	return Q(max(a.lx,b.lx),min(a.rx,b.rx),max(a.ly,b.ly),min(a.ry,b.ry));
}
Q minus_one(Q a){
	return Q(a.lx+1,a.rx-1,a.ly+1,a.ry-1);
}
bool sd(Q a,Pnt b){
	return((b.x==a.lx||b.x==a.rx)&&(a.ly<=b.y&&b.y<=a.ry))
    ||((b.y==a.ly||b.y==a.ry)&&(a.lx<=b.x&&b.x<=a.rx));
}
bool chk(Q a){
	return a.lx<=a.rx&&a.ly<=a.ry;
}
struct Node{
	int x,y,z;
	Node (int _x=0,int _y=0,int _z=0):
		x(_x),y(_y),z(_z){}
};
vector<Node>idx;
struct BIT{
	vector<long long>t;
	BIT(){}
	BIT(int x){
		t=vector<long long>(x+1,0ll);
	}
	int lowbit(int x){return x&-x;}
	void mdf(int x,int v){
		while(x<=m*7)
			t[x]+=v,x+=lowbit(x);
	}
	long long pfx(int x){
		long long ans=0ll;
		while(x>0)
			ans+=t[x],x-=lowbit(x);
		return ans;
	}
	long long qry(int l,int r){
		return pfx(r)-pfx(l-1);
	}
}t1,t2;
struct Seg{
	int l,r,f,id;
	Seg (int _l=0,int _r=0,int _f=0,int _id=0):
		l(_l),r(_r),f(_f),id(_id){}
};
vector<Seg>seg[M*7];
vector<int>pnt[M*7];
vector<long long>cnt,sum;
void cal(){
	for(int i=0;i<=m*7;i++)
		seg[i].clear(),pnt[i].clear();
	cnt=vector<long long>((int)sq.size(),0ll);
	sum=vector<long long>((int)sq.size(),0ll);
	for(int i=0;i<(int)sq.size();i++)
		if(chk(sq[i])){
			seg[sq[i].lx-1].push_back(Seg(sq[i].ly,sq[i].ry,-1,i));
			seg[sq[i].rx].push_back(Seg(sq[i].ly,sq[i].ry,1,i));
		}
	for(int i=1;i<=n;i++)
		pnt[p[i].x].push_back(i);
	t1=BIT(m*7),t2=BIT(m*7);
	for(int i=1;i<=m*7;i++){
		for(auto j:pnt[i]){
			t1.mdf(p[j].y,1);
			t2.mdf(p[j].y,val[j]);
		}
		for(auto j:seg[i]){
			cnt[j.id]+=j.f*t1.qry(j.l,j.r);
			sum[j.id]+=j.f*t2.qry(j.l,j.r);
		}
	}return;
}
map<Pnt,int>mp;
long long num[M*2]={0},num_cnt[M*2]={0};
long long upd(){
	long long tot=0ll;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			int d=dis2(p[i],p[j]);
			if(isGood[d]){
				tot+=num[d]+val[j]*num_cnt[d]+num_cnt[d]*val[i];
				num[d]+=val[j];
				num_cnt[d]++;
			}
		}
		for(int j=1;j<=n;j++){
			int d=dis2(p[i],p[j]);
			if(isGood[d]){
				num[d]-=val[j];
				num_cnt[d]--;
			}
		}
	}
	return tot;
}
void slv(){
	cin>>n>>m;
	mp.clear();
	for(int i=1;i<=n;i++)
		cin>>p[i].x>>p[i].y,p[i]=dec(p[i]);
	for(int i=1;i<=n;i++)
		cin>>val[i],mp[p[i]]=i;
	sq.clear();
	idx.clear();
	long long ans3=0ll;
	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++){
			int d=dis2(p[i],p[j]);
			if(isGood[d]){
				Q A=build_up(p[i],d),B=build_up(p[j],d);
				Q C=intersect(A,B),D=minus_one(C);
				idx.push_back(Node(i,j,(int)sq.size()));
				sq.push_back(A),sq.push_back(B),sq.push_back(C),sq.push_back(D);
				if(chk(C)){
					Pnt a=Pnt(C.lx,C.ly),b=Pnt(C.lx,C.ry);
					Pnt c=Pnt(C.rx,C.ly),dd=Pnt(C.rx,C.ry);
					int x=mp[a];
					if(x!=0&&x!=i&&x!=j&&sd(A,a)&&sd(B,a))
						ans3+=val[i]+val[j]+val[x];
					x=mp[b];
					if(x!=0&&x!=i&&x!=j&&sd(A,b)&&sd(B,b))
						ans3+=val[i]+val[j]+val[x];
					x=mp[c];
					if(x!=0&&x!=i&&x!=j&&sd(A,c)&&sd(B,c))
						ans3+=val[i]+val[j]+val[x];
					x=mp[dd];
					if(x!=0&&x!=i&&x!=j&&sd(A,dd)&&sd(B,dd))
						ans3+=val[i]+val[j]+val[x];
				}
			}
		}
	ans3/=3;
	long long ans2=upd()-ans3*3;
	cal();
	long long ans1=0ll;
	for(auto i:idx){
		long long Cnt=0ll,Sum=0ll;
		Cnt=cnt[i.z]+cnt[i.z+1]-cnt[i.z+2]-cnt[i.z+3]-2;
		Sum=sum[i.z]+sum[i.z+1]-sum[i.z+2]-sum[i.z+3]-(val[i.x]+val[i.y]);
 		ans1+=Cnt*(val[i.x]+val[i.y])+Sum;
	}
	cout<<ans1-ans2-2*ans3<<"\n";
	return;
}
int main(){
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
	init();
	int T;
	cin>>T;
	while(T--)slv();
	return 0;
}
